package com.senven.shoppingcar.service.impl;


import com.alibaba.fastjson.JSON;
import com.senven.common.entity.Branch;
import com.senven.common.entity.Goods;
import com.senven.common.entity.ShoppingTrolley;
import com.senven.common.utils.WebUtils;
import com.senven.shoppingcar.mapper.ShoppingTrolleyMapper;
import com.senven.shoppingcar.service.IShoppingTrolleyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.senven.shoppingcar.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周世玉
 * @since 2022-08-31
 */
@Service
public class ShoppingTrolleyServiceImpl extends ServiceImpl<ShoppingTrolleyMapper, ShoppingTrolley> implements IShoppingTrolleyService {

    @Autowired
    private ShoppingTrolleyMapper shoppingTrolleyMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public CommonResult selectAll(String userid) {
        //查询商品信息
        List<ShoppingTrolley> slist = shoppingTrolleyMapper.selectShoppingAll(userid);
        //查询购物车的店铺信息
        List<Branch> branches = shoppingTrolleyMapper.selectBranchAll(userid);

        for(Branch b: branches){
            List<ShoppingTrolley> goods = new ArrayList();
            for (ShoppingTrolley s : slist){
                if (b.getBranchid()==s.getBid()){
                    goods.add(s);
                    b.setGoods(goods);
                }
            }
        }
        return new CommonResult(2000,"查询成功",branches);
    }

    //增加商品的数量
    @Override
    public CommonResult updateAddNum(Integer gid,Integer bid) {
        int i = shoppingTrolleyMapper.updateAdd(gid,bid);
        if(i>0) {
            return new CommonResult(2000, "增加成功", i);
        }
        return new CommonResult(5000,"增加失败",null);
    }

    //服务调用商品数量增加方法
    @Override
    public Integer updateApiAddNum(Integer gid, Integer bid) {
        int i = shoppingTrolleyMapper.updateAdd(gid, bid);
        return i;
    }

    //减少商品的数量
    @Override
    public CommonResult updateReduceNum(Integer gid, Integer bid) {
        int i = shoppingTrolleyMapper.updateReduce(gid, bid);
        if(i>0) {
            return new CommonResult(2000, "减少成功", i);
        }
        return new CommonResult(5000,"减少失败",null);
    }

    //服务调用商品数量减少方法
    @Override
    public Integer updateApiReduceNumNum(Integer gid, Integer bid) {
        int i = shoppingTrolleyMapper.updateReduce(gid, bid);
        return i;
    }
//    从购物车中删除

    @Override
    public CommonResult delCat(Integer ctid) {
        int i = shoppingTrolleyMapper.updateIsDeleted(ctid);
        if (i==1){
            return new CommonResult(2000,"删除成功",null);
        }else{
            return new CommonResult(5000,"删除成功",null);
        }

    }
//根据店铺id和菜品id查询购物车
    @Override
    public CommonResult getCat(Integer branchid, Integer goodsid) {
        List<ShoppingTrolley> getCat = shoppingTrolleyMapper.getCat(branchid, goodsid);
        return (CommonResult) getCat;
    }

    //逻辑删除
    @Override
    public CommonResult deleteIsDeleted(List<Integer> stIds) {
        Integer sum = 0;
        for (Integer stId: stIds ) {
            Integer i = shoppingTrolleyMapper.updateIsDeleted(stId);
            sum = i+sum;
        }
        if(sum>0){
            return new CommonResult(2000,"删除成功",sum);
        }
        return new CommonResult(5000,"逻辑删除失败",null);
    }

    //查询每个店的销量
    @Override
    public CommonResult shopSalesAll(String start,String end) {
        List<Branch> list = shoppingTrolleyMapper.selectShopSalesAll(start,end);
        List name = new ArrayList();
        List num = new ArrayList();
        for (Branch b: list) {
            name.add(b.getBranchname());
            num.add(b.getNum());
        }
        List l = new ArrayList();
        l.add(name);
        l.add(num);
        return new CommonResult(2000,"查询成功",l);
    }

    //查询各个店铺下的各个商品销量
    @Override
    public CommonResult findDishesSales(Map map) {
        String token = WebUtils.getResult().getHeader("token");
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String json = forValue.get(token);
        Map o = JSON.parseObject(json, Map.class);

        Map user = (Map) o.get("user");

        Object id = user.get("id");
        System.out.println("id=========>"+id);

        String roleid = shoppingTrolleyMapper.selecByIdRole(id);
        String branchid = shoppingTrolleyMapper.selecBranchid(id);
        System.out.println("branchid==============>"+branchid);

        map.put("roleid",roleid);
        map.put("branchid",branchid);

        List<Goods> list = shoppingTrolleyMapper.selectDisheseSales(map);
        List name = new ArrayList();
        List num = new ArrayList();
        for (Goods g: list) {
            name.add(g.getGoodsname());
            num.add(g.getNum());
        }
        List l = new ArrayList();
        l.add(name);
        l.add(num);
        return new CommonResult(2000,"查询商品销量成功",l);
    }

    //查询店铺信息
    @Override
    public CommonResult findBranchName(Object token) {
        token = WebUtils.getResult().getHeader("token");
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String json = forValue.get(token);
        Map o = JSON.parseObject(json, Map.class);

        Map user = (Map) o.get("user");

        Object id = user.get("id");
        System.out.println("id=========>"+id);

        String roleid = shoppingTrolleyMapper.selecByIdRole(id);

        String branchid = shoppingTrolleyMapper.selecBranchid(id);

        List<Branch> list = shoppingTrolleyMapper.selectBranchIdAndName(roleid,branchid);

        return new CommonResult(2000,"查询店铺信息成功",list);
    }


    //    添加进入购物车
    @Override
    public CommonResult addCat(Integer branchid, Integer goodsid, Integer stNum, Integer goodsPrice, String uid) {
            Boolean isDeleted = false;
            int i =shoppingTrolleyMapper.addCat(branchid,goodsid,stNum,isDeleted,goodsPrice,uid);
            if (i==1){
                return new CommonResult(2000,"添加成功",null);
            }else{
                return new CommonResult(5000,"添加失败",null);
            }
        }

}


























